package com.buddies.lottery.data.repo;

import com.buddies.lottery.App;
import com.buddies.lottery.data.HandleResultHelper;
import com.buddies.lottery.data.RetrofitManager;
import com.buddies.lottery.data.api.LotteryApi;
import com.buddies.lottery.data.bean.BetItem;
import com.buddies.lottery.data.bean.Lottery;
import com.buddies.lottery.data.bean.LotteryBetRecord;
import com.buddies.lottery.data.bean.LotteryCategory;
import com.buddies.lottery.data.bean.LotteryReward;
import com.buddies.lottery.data.resp.HomeResponse;
import com.buddies.lottery.data.resp.LotteryConfigResponse;
import com.buddies.lottery.data.resp.LotteryDetailResponse;
import com.buddies.lottery.data.resp.LotteryTabOptionsResponse;
import com.buddies.lottery.data.resp.LotteryTrendResponse;
import com.buddies.lottery.data.resp.PostBetResponse;
import com.buddies.lottery.data.result.PageResult;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * Description:
 * Date: 2018/2/24
 * Author: xuliang
 */
public class LotteryRepo {

    private LotteryApi mLotteryApi;

    public LotteryRepo() {
        mLotteryApi = RetrofitManager.getInstance().create(LotteryApi.class);
    }

    public Observable<HomeResponse> getHome() {
        return mLotteryApi.getHome().compose(HandleResultHelper.<HomeResponse>handleResult());
    }

    public Observable<PageResult<Lottery>> getSimpleLotteryList() {
        return mLotteryApi.getSimpleLotteryList().compose(HandleResultHelper.<PageResult<Lottery>>handleResult());
    }

    public Observable<PageResult<Lottery>> getLotteryList(int lotteryCategoryId) {
        return mLotteryApi.getLotteryList(lotteryCategoryId).compose(HandleResultHelper.<PageResult<Lottery>>handleResult());
    }

    public Observable<LotteryDetailResponse> getLotteryDetail(int lotteryId) {
        return mLotteryApi.getLotteryDetail(lotteryId).compose(HandleResultHelper.<LotteryDetailResponse>handleResult());
    }

    public Observable<PageResult<LotteryCategory>> getLotteryCategoryList() {
        return mLotteryApi.getLotteryCategoryList().compose(HandleResultHelper.<PageResult<LotteryCategory>>handleResult());
    }

    public Observable<LotteryTabOptionsResponse> getLotteryTabOptions(String lotteryTabQueryId) {
        return mLotteryApi.getLotteryTabOptions(lotteryTabQueryId).compose(HandleResultHelper.<LotteryTabOptionsResponse>handleResult());
    }

    public Observable<PostBetResponse> postBet(int lotteryId, String period, List<BetItem> betItems) {
        return mLotteryApi.postBet(lotteryId, period, App.getGson().toJson(betItems)).compose(HandleResultHelper.<PostBetResponse>handleResult());
    }

    public Observable<PageResult<LotteryBetRecord>> getLotteryBetRecord(Map<String, Object> params, int page) {
        return mLotteryApi.getLotteryBetRecord((int) params.get("lotteryId"), (String) params.get("period"), (String) params.get("status"), page).compose(HandleResultHelper.<PageResult<LotteryBetRecord>>handleResult());
    }

    public Observable<LotteryBetRecord> getLotteryBetDetail(int betId) {
        return mLotteryApi.getLotteryBetDetail(betId).compose(HandleResultHelper.<LotteryBetRecord>handleResult());
    }

    public Observable<PageResult<LotteryReward>> getLotteryHistory(Map<String, Object> params, int page) {
        return mLotteryApi.getLotteryHistory((int) params.get("lotteryId"), page).compose(HandleResultHelper.<PageResult<LotteryReward>>handleResult());
    }

    public Observable<LotteryTrendResponse> getLotteryTrend(Map<String, Object> params, int page) {
        return mLotteryApi.getLotteryTrend((int) params.get("lotteryId"), page).compose(HandleResultHelper.<LotteryTrendResponse>handleResult());
    }

    public Observable<LotteryConfigResponse> getLotteryConfig() {
        return mLotteryApi.getLotteryConfig().compose(HandleResultHelper.<LotteryConfigResponse>handleResult());
    }
}
